package com.example.happ.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.happ.mapper.PriceInfoMapper;
import com.example.happ.mapper.StoreInfoMapper;
import com.example.happ.pojo.Price;
import com.example.happ.pojo.Store;
import com.example.happ.service.StoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class StoreServiceImpl implements StoreService {
    @Resource
    private StoreInfoMapper storeInfoMapper;
    @Resource
    private PriceInfoMapper priceInfoMapper;

    @Override
    public boolean updateStore(Map<String, String> requestBody) {
        Store store = new Store();
        store.setAddress(requestBody.get("address"));
        store.setPhone(requestBody.get("phone"));
        store.setIsEnabled(Integer.valueOf(requestBody.get("enabled")));
        store.setReviewedAt(Integer.valueOf(requestBody.get("review")));
        store.setSubmit(Integer.valueOf(requestBody.get("submit")));
        System.out.println(store.getReviewedAt());
        UpdateWrapper<Store> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",Integer.valueOf(requestBody.get("storeId")));
        store.setUpdatedAt(new Date());
        return storeInfoMapper.update(store,updateWrapper)!=0?true:false;
    }

    @Override
    public Store findStore(Integer id) {
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("merchant_id",id);
        System.out.println(storeInfoMapper.selectOne(queryWrapper));
        return storeInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Store updateStore1(Map<String, String> requestBody) {
        Store store = new Store();
        store.setName(requestBody.get("name"));
        store.setAvatar(requestBody.get("avatar"));
        store.setPlatformCustom(Integer.valueOf(requestBody.get("custom")));
        store.setPlatformMeituan(Integer.valueOf(requestBody.get("meituan")));
        store.setPlatformEleme(Integer.valueOf(requestBody.get("eleme")));
        UpdateWrapper<Store> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",Integer.valueOf(requestBody.get("storeId")));
        storeInfoMapper.update(store,updateWrapper);
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("id",Integer.valueOf(requestBody.get("storeId")));
        store = storeInfoMapper.selectOne(queryWrapper);
        System.out.println("门店基本信息修改获取到的值："+store);
        return store;
    }

    @Override
    public boolean updatePrice(Map<String, String> requestBody,Price price) {
        price.setMaxAmount(Double.parseDouble(requestBody.get("max")));
        price.setMinAmount(Double.parseDouble(requestBody.get("min")));
        price.setAutoCashback(Integer.valueOf(requestBody.get("auto")));
        price.setDefaultPrize(Integer.valueOf(requestBody.get("price")));
        price.setUpdatedAt(new Date());
        UpdateWrapper<Price> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("store_id",Integer.valueOf(requestBody.get("storeId")));
        updateWrapper.eq("type",requestBody.get("type"));
        return priceInfoMapper.update(price,updateWrapper)!=0?true:false;
    }

    @Override
    public Price findPrice(String type, String storeId) {
        System.out.println("随机红包信息访问请求收到的type:"+type);
        System.out.println("随机红包信息访问请求收到的storeId:"+storeId);
        QueryWrapper<Price> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("type",type);
        queryWrapper.eq("store_id",Integer.valueOf(storeId));
        Price price = priceInfoMapper.selectOne(queryWrapper);
        return price;
    }

    @Override
    public boolean insertPrice(Map<String, String> requestBody) {
        Price price = new Price();
        price.setMaxAmount(Double.parseDouble(requestBody.get("max")));
        price.setMinAmount(Double.parseDouble(requestBody.get("min")));
        price.setAutoCashback(Integer.valueOf(requestBody.get("auto")));
        price.setDefaultPrize(Integer.valueOf(requestBody.get("price")));
        price.setType(requestBody.get("type"));
        price.setCreatedAt(new Date());
        price.setStoreId(Integer.valueOf(requestBody.get("storeId")));
        return priceInfoMapper.insert(price)!=0?true:false;
    }
}
